<?php

namespace app\admin\controller;
use app\admin\model\Model;
use app\admin\model\Themes;
use Category\Category;
use think\Request;
use app\admin\model\Column as columnModel;

class Column extends Common
{
    /**
     * 显示内容模型列表
     *
     * @return \think\Response
     */
    public function modelIndex()
    {
       $model =  Model::order('sort','desc')->paginate(5);
       $this->assign('model',$model);
        return $this->fetch();
    }

    /**
     * 显示创建模型表单页.
     *
     * @return \think\Response
     */
    public function createModel(Request $request)
    {
        $request->isPost() AND $this->saveModel();
        return $this->fetch();
    }

    /**
     *  保存新建的模型
     */
    private function saveModel()
    {
       $error = $this->validate(input('post.'),'cloumn.modelAddEdit');
       if ($error !== true){
           $this->error($error);
       }
       $model =  Model::create(input('post.'),true);
        if ($model){
            $this->success('新增[ '.$model->name.' ]模型成功！',url('modelIndex'));
        }
            $this->error('新增模型失败！');
    }

    /**
     * 更新排序
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function sort($sort)
    {
        (!is_array($sort) OR empty($sort)) AND $this->error('没有要排序项！');
        foreach ($sort as $k=>$v){
            Model::update([
                'model_id'  =>  (int)$k,
                'sort'      =>  (int)$v
            ]);
        }
        $this->success('已排序~');
    }

    /**
     * 显示编辑模型表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function editModel(Request $request,$model_id)
    {
       ($model =  Model::get($model_id)) OR $this->error('此模型不存在！');
        $request->isPost() AND $this->updateModel($model);
       $this->assign('model',$model);
       return $this->fetch();
    }

    /** 保存更新的模型
     * @param $model
     */
    private function updateModel($model)
    {
        $error = $this->validate(input('post.'),'cloumn.modelAddEdit');
        if ($error !== true){
             $this->error($error);
        }
        $data = input('post.');
        $model->name = $data['name'];
        $model->tablename = $data['tablename'];
        $model->description = $data['description'];
        $model->status      = $data['status'];
        $model->sort      = $data['sort'];
        if ($model->save()){
             $this->success('模型'.$model->name.'更新成功！',url('modelIndex'));
        }
         $this->error('模型'.$model->name.'更新失败！');
    }

    /**
     * 删除指定模型
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deleteModel($model_id)
    {
        if (Model::destroy($model_id)){
            $this->success('删除成功！',url('modelIndex'));
        }
        $this->error('删除失败！');
    }


    public function lists(){
        $category = columnModel::all([],'model');
        $this->assign('category',Category::toLevel($category,0,'column_id'));
        return $this->fetch();
    }

    /** 添加 栏目
     * @return mixed
     */
    public function createColumn(Request $request){
        $request->isPost() AND $this->saveColumn();
        // 需要 模型
        $model = Model::order('sort','desc')->where(['status' => 1])->select();
        //需要 父 栏目
        $category = columnModel::all([],['model']);
        $this->assign('category',Category::toLevel($category,0,'column_id'));
        $this->assign('model',$model);
        //【新增】 模板列表
        $this->assign('themes',Themes::defaultThemes());
        return $this->fetch();
    }

    /**
     * 新增保存
     */
    private function saveColumn(){
        $error = $this->validate(input('post.'),'cloumn.columnAddEdit');
        if ($error !== true){
            $this->error($error);
        }
        $cloumn =  columnModel::create(input('post.'),true);
        if ($cloumn){
            columnModel::updateNavMenu();
            $this->success('新增[ '.$cloumn->name.' ]栏目成功！',url('lists'));
        }
            $this->error('新增栏目失败！');
    }

    /**
     * 编辑
     */
    public function editColumn(Request $request,$column_id){
        ($column = columnModel::get($column_id)) OR $this->error('栏目不存在！无法更新！');
        $request->isPost() AND $this->updateColumn($column);
        // 需要 模型
        $model = Model::order('sort','desc')->where(['status' => 1])->select();
        //需要 父 栏目
        $category = columnModel::all([],['model']);
        // 查出所有分类
        $this->assign('category',Category::toLevel($category,0,'column_id'));
        // 查出所有的子 id
        $this->assign('childId',Category::getChildsId($category,$column_id,true,'column_id'));
        $this->assign('model',$model);
        $this->assign('column',$column);
        //【新增】 模板列表
        $this->assign('themes',Themes::defaultThemes());
        return $this->fetch();
    }

    /**
     * 编辑 保持
     */
    private function updateColumn($column){
        $error = $this->validate(input('post.'),'cloumn.columnAddEdit');
        if ($error !== true){
            $this->error($error);
        }
//        ($column = columnModel::get($column_id)) OR $this->error('此栏目不存在,无法更新！');
        if ($column->isUpdate()->allowField(true)->save(input('post.'))){
            columnModel::updateNavMenu();
            $this->success('栏目'.$column->name.'更新成功！',url('lists'));
        }
        $this->error('栏目'.$column->name.'更新失败！');
    }

    /** 删除 栏目
     * @param $column_id
     */
    public function deleteColumn($column_id)
    {
        $category = columnModel::all([],['model']);
        $childId  = Category::getChildsId($category,$column_id,true,'column_id');
        if (columnModel::destroy($childId)){
            //删除分类下  所有文章

            //  ing......
            columnModel::updateNavMenu();
            $this->success('删除成功！',url('lists'));
        }
        $this->error('删除失败！');
    }

    /**
     * 更新排序
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function columnSort($sort)
    {
        (!is_array($sort) OR empty($sort)) AND $this->error('没有要排序项！');
        foreach ($sort as $k=>$v){
           columnModel::update([
                'column_id'  =>  (int)$k,
                'sort'       =>  (int)$v
            ]);
        }
        $this->success('已排序~');
    }

















}
